using HKB.Common;
using System;
using System.Data;
using System.Text;
using System.Web;
namespace HKB.Web
{
	public class GroupBll
	{
		private Conn conn;
		public GroupBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public GroupBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetGroups()
		{
			return this.conn.GetDataTable("Select *,(Select Count(UserID) From [HL_User] Where HL_User.GroupID=HL_Group.GroupID) as MemberNum From [HL_Group]");
		}
		public DataTable GetGroupsNameID()
		{
			return this.conn.GetDataTable("Select GroupName,GroupID From [HL_Group]");
		}
		public DataTable GetGroupsNameByID(string groupid)
		{
			return this.conn.GetDataTable("Select GroupName From [HL_Group] Where GroupID in (" + groupid + ")");
		}
		public DataTable GetGroupPowerByID(int id)
		{
			return this.conn.GetDataTable("Select GroupPower,Locked,IsShow From [HL_Group] Where GroupID=" + id);
		}
		public DataTable GetGroupByGroupID(int GroupID)
		{
			return this.conn.GetDataTable("Select top 1  * From [HL_Group] Where ([GroupID] =" + GroupID.ToString() + ")");
		}
		public int CountMemberNumByGroupID(int GroupID)
		{
			return this.conn.ExecuteCount("Select count(*) from [HL_User] Where GroupID=" + GroupID);
		}
		public int CountGroup()
		{
			return this.conn.ExecuteCount("Select count(*) from [HL_Group]");
		}
		public int GetGID()
		{
			object scalar = this.conn.GetScalar("Select top 1 GroupID From [HL_Group]", new IDbDataParameter[0]);
			int result;
			if (scalar != null && scalar != DBNull.Value)
			{
				result = (int)scalar;
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public bool CheckGroupPower(string GroupPower, string key)
		{
			bool result;
			if (GroupPower == "")
			{
				result = false;
			}
			else
			{
				string[] array = GroupPower.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i].Trim() == key)
					{
						result = true;
						return result;
					}
				}
				result = false;
			}
			return result;
		}
		public bool AddGroup(string GroupName, string GroupIntro, int MaxMessage, int AllowUploadSize, int SendMessage, int ShoppingDiscount, int OverDraft, int TransferCharge, int AllowSycharArticle, string GroupPower, int GroupMemberNum, int maxmessagesend, int maxproduct, int maxpaytype, int maxdelitype, int maxmyfav, int maxteambuyrate, float paytip, int IsShow, int Locked)
		{
			string commandText = "insert into HL_Group(  GroupName, GroupIntro, MaxMessage, AllowUploadSize, SendMessage, ShoppingDiscount, OverDraft, TransferCharge, AllowSycharArticle, GroupMemberNum, GroupPower, maxmessagesend, maxproduct, maxpaytype, maxdelitype, maxmyfav, maxteambuyrate, paytip,IsShow,Locked) values (@GroupName,@GroupIntro,@MaxMessage,@AllowUploadSize,@SendMessage,@ShoppingDiscount,@OverDraft,@TransferCharge,@AllowSycharArticle,@GroupMemberNum,@GroupPower,@maxmessagesend,@maxproduct,@maxpaytype,@maxdelitype,@maxmyfav,@maxteambuyrate,@paytip,@IsShow,@Locked);select @@IDENTITY";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@GroupName", SqlDbType.NVarChar, GroupName),
				this.conn.CreateParameter("@GroupIntro", SqlDbType.Text, GroupIntro),
				this.conn.CreateParameter("@MaxMessage", SqlDbType.Int, MaxMessage),
				this.conn.CreateParameter("@AllowUploadSize", SqlDbType.Int, AllowUploadSize),
				this.conn.CreateParameter("@SendMessage", SqlDbType.Int, SendMessage),
				this.conn.CreateParameter("@ShoppingDiscount", SqlDbType.Int, ShoppingDiscount),
				this.conn.CreateParameter("@OverDraft", SqlDbType.Int, OverDraft),
				this.conn.CreateParameter("@TransferCharge", SqlDbType.NVarChar, TransferCharge),
				this.conn.CreateParameter("@AllowSycharArticle", SqlDbType.Int, AllowSycharArticle),
				this.conn.CreateParameter("@GroupMemberNum", SqlDbType.Int, GroupMemberNum),
				this.conn.CreateParameter("@GroupPower", SqlDbType.NVarChar, GroupPower),
				this.conn.CreateParameter("@maxmessagesend", SqlDbType.Int, maxmessagesend),
				this.conn.CreateParameter("@maxproduct", SqlDbType.Int, maxproduct),
				this.conn.CreateParameter("@maxpaytype", SqlDbType.Int, maxpaytype),
				this.conn.CreateParameter("@maxdelitype", SqlDbType.Int, maxdelitype),
				this.conn.CreateParameter("@maxmyfav", SqlDbType.Int, maxmyfav),
				this.conn.CreateParameter("@maxteambuyrate", SqlDbType.Int, maxteambuyrate),
				this.conn.CreateParameter("@paytip", SqlDbType.Float, paytip),
				this.conn.CreateParameter("@IsShow", SqlDbType.Int, IsShow),
				this.conn.CreateParameter("@Locked", SqlDbType.Int, Locked)
			};
			return Convert.ToInt32(this.conn.GetScalar(commandText, parameters)) > 0;
		}
		public bool UpdateGroup(int GroupID, string GroupName, string GroupIntro, int MaxMessage, int AllowUploadSize, int SendMessage, int ShoppingDiscount, int OverDraft, int TransferCharge, int AllowSycharArticle, int GroupMemberNum, string GroupPower, int maxmessagesend, int maxproduct, int maxpaytype, int maxdelitype, int maxmyfav, int maxteambuyrate, float paytip, int IsShow, int Locked)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("update HL_Group set ");
			stringBuilder.Append("GroupName=@GroupName,");
			stringBuilder.Append("GroupIntro=@GroupIntro,");
			stringBuilder.Append("MaxMessage=@MaxMessage,");
			stringBuilder.Append("AllowUploadSize=@AllowUploadSize,");
			stringBuilder.Append("SendMessage=@SendMessage,");
			stringBuilder.Append("ShoppingDiscount=@ShoppingDiscount,");
			stringBuilder.Append("OverDraft=@OverDraft,");
			stringBuilder.Append("TransferCharge=@TransferCharge,");
			stringBuilder.Append("AllowSycharArticle=@AllowSycharArticle,");
			stringBuilder.Append("GroupMemberNum=@GroupMemberNum,");
			stringBuilder.Append("GroupPower=@GroupPower,");
			stringBuilder.Append("maxmessagesend=@maxmessagesend,");
			stringBuilder.Append("maxproduct=@maxproduct,");
			stringBuilder.Append("maxpaytype=@maxpaytype,");
			stringBuilder.Append("maxdelitype=@maxdelitype,");
			stringBuilder.Append("maxmyfav=@maxmyfav,");
			stringBuilder.Append("maxteambuyrate=@maxteambuyrate,");
			stringBuilder.Append("paytip=@paytip,");
			stringBuilder.Append("IsShow=@IsShow,");
			stringBuilder.Append("Locked=@Locked");
			stringBuilder.Append(" where GroupID=@GroupID ");
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@GroupName", SqlDbType.NVarChar, GroupName),
				this.conn.CreateParameter("@GroupIntro", SqlDbType.Text, GroupIntro),
				this.conn.CreateParameter("@MaxMessage", SqlDbType.Int, MaxMessage),
				this.conn.CreateParameter("@AllowUploadSize", SqlDbType.Int, AllowUploadSize),
				this.conn.CreateParameter("@SendMessage", SqlDbType.Int, SendMessage),
				this.conn.CreateParameter("@ShoppingDiscount", SqlDbType.Int, ShoppingDiscount),
				this.conn.CreateParameter("@OverDraft", SqlDbType.Int, OverDraft),
				this.conn.CreateParameter("@TransferCharge", SqlDbType.NVarChar, TransferCharge),
				this.conn.CreateParameter("@AllowSycharArticle", SqlDbType.Int, AllowSycharArticle),
				this.conn.CreateParameter("@GroupMemberNum", SqlDbType.Int, GroupMemberNum),
				this.conn.CreateParameter("@GroupPower", SqlDbType.NVarChar, GroupPower),
				this.conn.CreateParameter("@maxmessagesend", SqlDbType.Int, maxmessagesend),
				this.conn.CreateParameter("@maxproduct", SqlDbType.Int, maxproduct),
				this.conn.CreateParameter("@maxpaytype", SqlDbType.Int, maxpaytype),
				this.conn.CreateParameter("@maxdelitype", SqlDbType.Int, maxdelitype),
				this.conn.CreateParameter("@maxmyfav", SqlDbType.Int, maxmyfav),
				this.conn.CreateParameter("@maxteambuyrate", SqlDbType.Int, maxteambuyrate),
				this.conn.CreateParameter("@paytip", SqlDbType.Float, paytip),
				this.conn.CreateParameter("@Locked", SqlDbType.Int, Locked),
				this.conn.CreateParameter("@GroupID", SqlDbType.Int, GroupID),
				this.conn.CreateParameter("@IsShow", SqlDbType.Int, IsShow)
			};
			return this.conn.Execute(stringBuilder.ToString(), parameters) > 0;
		}
		public bool DeleteGroup(int GroupID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@GroupID", DbType.Int32, GroupID);
			int num = this.conn.Execute("DELETE From [HL_Group] Where ([GroupID] = @GroupID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
			return num == 1;
		}
		public bool OwnerPower(int salerID, string powername)
		{
			bool result = false;
			if (salerID > 0)
			{
				string sql = "select GroupPower from HL_User,HL_Group where HL_User.GroupID=HL_Group.GroupID and HL_User.UserID=" + salerID;
				DataTable dataTable = this.conn.GetDataTable(sql);
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					if (dataTable.Rows[0]["GroupPower"] != DBNull.Value)
					{
						string text = dataTable.Rows[0]["GroupPower"].ToString();
						string[] array = text.Split(new char[]
						{
							','
						});
						for (int i = 0; i < array.Length; i++)
						{
							if (array[i] == powername)
							{
								result = true;
								break;
							}
						}
					}
				}
			}
			return result;
		}
		public bool OwnerPowerTransaction(int salerID, string powername)
		{
			bool result = false;
			if (salerID > 0)
			{
				string commandText = "select GroupPower from HL_User,HL_Group where HL_User.GroupID=HL_Group.GroupID and HL_User.UserID=" + salerID;
				object obj = this.conn.ExecuteTransactionScalar(commandText);
				if (obj != null)
				{
					string text = obj.ToString();
					string[] array = text.Split(new char[]
					{
						','
					});
					for (int i = 0; i < array.Length; i++)
					{
						if (array[i] == powername)
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}
		public bool OwnerStockPower(int salerID)
		{
			return this.OwnerPowerTransaction(salerID, "stockmanage");
		}
		public bool OwnerPromotionPower(int salerID)
		{
			return this.OwnerPowerTransaction(salerID, "promotionmanage");
		}
		public bool OwnerPowerFinancemanage(int salerID)
		{
			return this.OwnerPowerTransaction(salerID, "financemanage");
		}
	}
}
